#include "Geometrist_Wrap.h"
#include "GeoOperation/UGBufferParam.h"
#include "GeoOperation/UGGeoOperator.h"

#ifdef __cplusplus  
extern "C" { 
#endif

RWRAP_API UGGeoRegion* RWRAP_CALL Geometrist_wrap_create_buffer(UGGeometry* pGeometry, UGdouble dLeft, UGdouble dRight,
																   const char* side, const char* end,
																   const char* unit, UGint nSegment,
																   UGPrjCoordSys* pPrj)
{
	if(pGeometry != NULL)
	{
		UGBufferParam bufferParam;
		UGString strSide;
		strSide.FromUTF8(side, strlen(side));

		if(strSide.CompareNoCase(_U("full")) == 0)
		{
			bufferParam.m_nSideType = UGBufferParam::UGFULL;
			if(!UGIS0(dLeft))
			{
				bufferParam.m_dBufferRadius = dLeft;
			}
			else if(!UGIS0(dRight))
			{
				bufferParam.m_dBufferRadius = dRight;
			}
			else
			{
				bufferParam.m_dBufferRadius = dLeft;
			}
		}
		else if(strSide.CompareNoCase(_U("left")) == 0)
		{
			bufferParam.m_nSideType = UGBufferParam::UGLEFT;
			bufferParam.m_dBufferRadius = dLeft;
		}
		else if(strSide.CompareNoCase(_U("right")) == 0)
		{
			bufferParam.m_nSideType = UGBufferParam::UGRIGHT;
			bufferParam.m_dBufferRadius = dRight;
		}
		else
		{
			bufferParam.m_nSideType = UGBufferParam::UGFULLDIFFR;
			bufferParam.m_dDiffBufferRadius[0] = dLeft;
			bufferParam.m_dDiffBufferRadius[1] = dRight;
		}

		UGString strEnd;
		strEnd.FromUTF8(end, strlen(end));
		if(strEnd.CompareNoCase(_U("flat")) == 0)
		{
			bufferParam.m_nEndType = UGBufferParam::UGFLAT;
		}
		else
		{
			bufferParam.m_nEndType = UGBufferParam::UGROUND;
		}
	
		UGint nUnit = nameToUnit(unit);
		bufferParam.m_nRaidusUnit = nUnit;
		bufferParam.m_nSemicircleSegments = nSegment;		
		UGGeoRegion geoRegion;
		if(UGGeoOperator::CreateBuffer(pGeometry, bufferParam, geoRegion, pPrj))
		{
			return new UGGeoRegion(geoRegion);
		}
		else
		{
			return NULL;
		}
	}
	return NULL;
}

RWRAP_API UGint nameToUnit(const char* name)
{
	UGString strName;
	strName.FromUTF8(name, strlen(name));
	if(strName.CompareNoCase(_U("METER")) == 0)
	{
		return AU_METER;
	}
	if(strName.CompareNoCase(_U("MILIMETER")) == 0)
	{
		return AU_MILIMETER;
	}
	if(strName.CompareNoCase(_U("CENTIMETER")) == 0)
	{
		return AU_CENTIMETER;
	}
	if(strName.CompareNoCase(_U("DECIMETER")) == 0)
	{
		return AU_DECIMETER;
	}
	if(strName.CompareNoCase(_U("KILOMETER")) == 0)
	{
		return AU_KILOMETER;
	}
	if(strName.CompareNoCase(_U("MILE")) == 0)
	{
		return AU_MILE;
	}
	if(strName.CompareNoCase(_U("INCH")) == 0)
	{
		return AU_INCH;
	}
	if(strName.CompareNoCase(_U("YARD")) == 0)
	{
		return AU_YARD;
	}
	if(strName.CompareNoCase(_U("FOOT")) == 0)
	{
		return AU_FOOT;
	}
	return AU_METER;
}

#ifdef __cplusplus
}
#endif